package atm05;

import atm04.FileUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 改密界面
 */
public class ChangePwdPanel extends JPanel implements ActionListener{
    private ATM05 atm05;
    private JLabel oldPwd = new JLabel("输入原密码：");
    private JLabel newPwd01 = new JLabel("输入新密码：");
    private JLabel newPwd02 = new JLabel("再次输入新密码：");
    private JPasswordField oldPwdText = new JPasswordField();
    private JPasswordField newPwd01Text = new JPasswordField();
    private JPasswordField newPwd02Text = new JPasswordField();
    private JButton sureBtn = new JButton(new ImageIcon("src/atm05/Images/buttonSure.jpg"));
    private JButton exitBtn = new JButton(new ImageIcon("src/atm05/Images/buttonOff.jpg"));
    private Font font = new Font("黑体",Font.BOLD,25);

    public ChangePwdPanel(ATM05 atm05) {
        this.atm05 = atm05;
        this.setLayout(null);

        oldPwd.setBounds(300,200,220,50);
        oldPwd.setFont(font);

        newPwd01.setBounds(300,300,220,50);
        newPwd01.setFont(font);

        newPwd02.setBounds(300,400,220,50);
        newPwd02.setFont(font);

        oldPwdText.setBounds(530,200,220,50);

        newPwd01Text.setBounds(530,300,220,50);

        newPwd02Text.setBounds(530,400,220,50);

        sureBtn.setBounds(420,520,140,50);
        sureBtn.addActionListener(this);

        exitBtn.setBounds(880,380,140,50);
        exitBtn.addActionListener(this);
        exitBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                atm05.showPanel("menu");
            }
        });

        this.add(oldPwd);
        this.add(newPwd01);
        this.add(newPwd02);
        this.add(oldPwdText);
        this.add(newPwd01Text);
        this.add(newPwd02Text);
        this.add(sureBtn);
        this.add(exitBtn);

        this.setOpaque(false);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        int index = atm05.getIndex();
        boolean flag = false;
        String truePwd1 = "";
        String truePwd2 ="";
        String opwd = String.valueOf(oldPwdText.getPassword());
         if (e.getSource() == sureBtn){
             if (atm05.getUsers().get(index).getPwd().equals(opwd)){
                 flag = true;
             }else {
                 JOptionPane.showMessageDialog(this,"旧密码输入错误");
                 oldPwdText.setText("");
                 newPwd01Text.setText("");
                 newPwd02Text.setText("");
                 return;
             }
             if (flag){
                 truePwd1 = String.valueOf(newPwd01Text.getPassword());
                 truePwd2 = String.valueOf(newPwd02Text.getPassword());
                 if (truePwd1.equals(truePwd2)){
                     JOptionPane.showMessageDialog(this,"密码修改成功！");
                     FileUtil.writeObject(atm05.getUsers(),ATM05.FILE_PATH);
                     atm05.getUsers().get(index).setPwd(truePwd2);
                     atm05.showPanel("login");
                 }else {
                     JOptionPane.showMessageDialog(this,"两次新密码输入不同，请重新输入");
                     oldPwdText.setText("");
                     newPwd01Text.setText("");
                     newPwd02Text.setText("");
                     return;
                 }
             }

         }else if (e.getSource() == exitBtn){
             oldPwdText.setText("");
             newPwd01Text.setText("");
             newPwd02Text.setText("");
             atm05.showPanel("menu");
         }
    }
}
